package com.xiaoshuidi.cloud.module.member.mq.listerner;

import cn.hutool.core.util.ObjectUtil;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventData;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventListener;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventTypeEnum;
import com.xiaoshuidi.cloud.framework.systemevent.core.data.LeaseSystemEventNewSign;
import com.xiaoshuidi.cloud.module.member.controller.admin.coupon.vo.NewSignCouponSendVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.coupon.vo.OldBringNewCouponSendVO;
import com.xiaoshuidi.cloud.module.member.enums.coupon.CouponTypeEnum;
import com.xiaoshuidi.cloud.module.member.pojo.user.MemberUser;
import com.xiaoshuidi.cloud.module.member.service.coupon.CouponCustomerService;
import com.xiaoshuidi.cloud.module.member.service.user.MemberUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;

@Component
@Slf4j
public class LeaseNewSignSystemEventListener implements LeaseSystemEventListener {
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private CouponCustomerService couponCustomerService;

    @Resource
    private PlatformTransactionManager transactionManager;

    @Resource
    private TransactionDefinition transactionDefinition;

    @Override
    public LeaseSystemEventTypeEnum getEventType() {
        return LeaseSystemEventTypeEnum.NEW_SIGN;
    }

    @Override
    public void onEvent(LeaseSystemEventData event) {
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        LeaseSystemEventNewSign data = (LeaseSystemEventNewSign) event;
        log.info("收到新签约消息 {}######################", JsonUtils.toJsonString(data));
        try {
            Long userId = data.getUserId();
            //查看这个人是不是有老客带的新客
            MemberUser user = memberUserService.getUser(userId);
            //发新签约的券
            NewSignCouponSendVO newSignCouponSendVO = new NewSignCouponSendVO()
                    .setNewSignDate(data.getSignDate())
                    .setApartmentId(data.getApartmentId())
                    .setBillPeriods(data.getRentOrderCount())
                    .setTenantId(data.getTenantId())
                    .setUserId(userId)
                    .setStartTime(data.getContractStartDate())
                    .setEndTime(data.getContractEndDate())
                    .setBusinessKey(CouponTypeEnum.NEW_SIGNING_DISCOUNT.getCode() + data.getContractNo());
            couponCustomerService.validNewSignSendCoupon(newSignCouponSendVO);
            if (user != null && ObjectUtil.isNotEmpty(user.getRecommendId())) {
                //校验老带新的recommend是否存在这个用户
                MemberUser oldUser = memberUserService.getUser(user.getRecommendId());
                if (ObjectUtil.isNotEmpty(oldUser)) {
                    //要发老带新的券
                    OldBringNewCouponSendVO oldBringNewCouponSendVO = new OldBringNewCouponSendVO()
                            .setNewApartmentId(data.getApartmentId())
                            .setNewUserId(userId).setOldUserId(user.getRecommendId())
                            .setTenantId(data.getTenantId())
                            .setBusinessKey(CouponTypeEnum.OLD_WITH_NEW_DISCOUNT.getCode() + data.getContractNo())
                            .setOldBringsNewDate(data.getSignDate())
                            .setStartTime(data.getContractStartDate())
                            .setEndTime(data.getContractEndDate());
                    couponCustomerService.validOldBringsNewSendCoupon(oldBringNewCouponSendVO);
                }
            }
            transactionManager.commit(transactionStatus);
        } catch (Exception e) {
            log.error("新签约发券异常");
            transactionManager.rollback(transactionStatus);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "新签约发券异常!");
        }
    }
}
